package com.yugao.fintech.antelope.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.system.assembly.SysMenuSceneAssembly;
import com.yugao.fintech.antelope.system.manager.SysMenuSceneManager;
import com.yugao.fintech.antelope.system.mapper.SysSceneMapper;
import com.yugao.fintech.antelope.system.model.entity.SysMenuScene;
import com.yugao.fintech.antelope.system.model.rqrs.menu.MenuSceneResp;
import com.yugao.fintech.antelope.system.model.rqrs.menu.MenuSceneSaveReq;
import com.yugao.fintech.antelope.system.model.rqrs.menu.MenuSceneUpdateReq;
import com.yugao.fintech.antelope.system.model.rqrs.menu.SceneListReq;
import com.yugao.fintech.antelope.system.service.SysMenuSceneService;
import com.yugao.fintech.framework.assistant.utils.CollectionUtils;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 场景
 */
@Slf4j
@Service
public class SysMenuSceneServiceImpl extends ServiceImpl<SysSceneMapper, SysMenuScene> implements IService<SysMenuScene>, SysMenuSceneService {
    @Autowired
    private SysMenuSceneAssembly sceneAssembly;
    @Autowired
    private SysMenuSceneManager sceneManager;

    /**
     * 查询 场景 所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @Override
    public List<MenuSceneResp> listScene(SceneListReq query) {
        LambdaQueryWrapper<SysMenuScene> lqw = SysMenuScene.lqw()
                .like(StringUtils.isNotEmpty(query.getName()), SysMenuScene::getName, query.getName())
                .orderByDesc(SysMenuScene::getCreatedAt);
        return this.list(lqw).stream().map(sceneAssembly::toSceneResp).collect(Collectors.toList());
    }

    /**
     * 保存 场景
     *
     * @param req 新增的记录
     * @return 返回影响行数
     */
    @Override
    public boolean saveScene(MenuSceneSaveReq req) {
        LambdaQueryWrapper<SysMenuScene> lqw = SysMenuScene.lqw().eq(SysMenuScene::getCode, req.getCode());
        if (this.count(lqw) > 0) {
            throw new BizException("标识符 {} 已存在", req.getCode());
        }

        SysMenuScene entity = sceneAssembly.toScene(req);
        entity.setId(null);
        return this.save(entity);
    }


    /**
     * 更新 场景
     *
     * @param req 修改的记录
     * @return 返回影响行数
     */
    @Override
    public boolean updateSceneById(MenuSceneUpdateReq req) {
        LambdaQueryWrapper<SysMenuScene> lqw = SysMenuScene.lqw().eq(SysMenuScene::getCode, req.getCode())
                .ne(SysMenuScene::getId, req.getId());
        if (this.count(lqw) > 0) {
            throw new BizException("标识符 {} 已存在", req.getCode());
        }
        SysMenuScene entity = sceneAssembly.toScene(req);
        sceneManager.cleanCacheByCode(entity.getCode());
        return this.updateById(entity);
    }

    /**
     * 通过id删除 场景
     *
     * @param id 删除的主键
     * @return 返回影响行数
     */
    @Override
    public boolean deleteSceneById(Long id) {
        SysMenuScene scene = this.getById(id);
        if (Objects.isNull(scene)) {
            return false;
        }
        sceneManager.cleanCacheByCode(scene.getCode());
        return this.removeById(id);
    }

    /**
     * 通过ids删除 场景
     *
     * @param ids 删除的主键
     * @return 返回影响行数
     */
    @Override
    public boolean deleteSceneByIds(List<Long> ids) {
        List<SysMenuScene> sceneList = this.listByIds(ids);
        if (CollectionUtils.isEmpty(sceneList)) {
            return false;
        }
        sceneList.stream().map(SysMenuScene::getCode).forEach(sceneManager::cleanCacheByCode);
        return this.removeByIds(ids);
    }
}
